﻿// 认证管理

var length;  //数据总条数
var start;  //从第几条数据开始显示
var end;  //到第几条数据结束显示
var pageCount;  //总页数
var jobNumber;  //当前操作的用户工号
var director;  //当前操作的用户上级负责人
var Ustate;  //当前操作的用户状态

// 解析URL
var parameter = window.location.href.split("?")[1];
var parameterList = parameter.split("&");
var username = decodeURIComponent(parameterList[0]);  //用户名
var department = decodeURIComponent(parameterList[1]);  //科室
var clinical_title = decodeURIComponent(parameterList[2]);  //临床职称
var teaching_title = decodeURIComponent(parameterList[3]);  //教学职称
var type = decodeURIComponent(parameterList[4]);  //类型
var source = decodeURIComponent(parameterList[5]);  //来源
var searchword = decodeURIComponent(parameterList[6]);  //姓名/联系方式
var state = decodeURIComponent(parameterList[7]);  //状态
var pageNo  = decodeURIComponent(parameterList[8]);  //页码
var pageSize = decodeURIComponent(parameterList[9]);  //每页显示数据数目
var timefrom = decodeURIComponent(parameterList[10]);
var timeto = decodeURIComponent(parameterList[11]);
console.log(parameterList);
$("#department").val(department);
$("#clinical_title").val(clinical_title);
$("#teaching_title").val(teaching_title);
$("#type").val(type);
$("#source").val(source);
$("#search_input").val(searchword);
$("#pageSize").val(pageSize+"条/页");
$("#timefrom").val(timefrom);
$("#timeto").val(timeto);

if(department != ""){
    $("#department").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#department").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(clinical_title != ""){
    $("#clinical_title").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#clinical_title").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(teaching_title != ""){
    $("#teaching_title").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#teaching_title").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(type != ""){
    $("#type").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#type").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(source != ""){
    $("#source").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#source").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(timefrom != ""){
    $("#timefrom").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#timefrom").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}
if(timeto != ""){
    $("#timeto").parent().mouseover(function () {
        $(this).find(".icon_clear").css('display','block');
    });
    $("#timeto").parent().mouseout(function () {
        $(this).find(".icon_clear").css('display','none');
    });
}

// 时间处理函数（补0）
function time_format(time){
    var timeList = time.split("-");
    var year = timeList[0];  //年
    var month = timeList[1];  //月
    var day = timeList[2];  //日
    if(month<10){
        month = "0"+month
    }
    if(day<10){
        day = "0"+day
    }
    time = year+"-"+month+"-"+day;
    return time;
}
// 日历插件
$('#calendarfrom').calendar({
    width: 200,
    height: 200,
    trigger: '#timefrom',
    zIndex: 999,
    format: 'yyyy-mm-dd',
    onSelected: function (view, date, data) {
        // 点击时触发
        // console.log('event: onSelected')
    },
    onClose: function (view, date, data) {
        // 点击后触发
        // console.log('event: onClose');
        timefrom = time_format($("#timefrom").val());
        $("#timefrom").val(timefrom);
        refresh();
    }
});
$('#calendarto').calendar({
    width: 200,
    height: 200,
    trigger: '#timeto',
    zIndex: 999,
    format: 'yyyy-mm-dd',
    onSelected: function (view, date, data) {
        // console.log('event: onSelected');
    },
    onClose: function (view, date, data) {
        // console.log('event: onClose');
        timeto = time_format($("#timeto").val());
        $("#timeto").val(timeto);
        refresh();
    }
});

// 根据URL中的用户名获取工号
$.ajax({
    type: "post",
    url: "/ysfwpt/selectUserInfoByName",
    async: false,
    data: {
        username: username
    },
    success: function(data) {
        // console.log("success");
        // console.log(data);
        var job_number = data.user.job_number;
        var user_type = data.user.type;
        // 根据获取到的工号以及输入框搜索词查询用户信息
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectUserByInputList2",
            data: {
                department: department,
                clinical_title: clinical_title,
                teaching_title: teaching_title,
                type: type,
                source: source,
                state: state,
                searchword: searchword,
                job_number: job_number,
                user_type: user_type,
                timefrom: timefrom,
                timeto: timeto
            },
            success: function (data) {
                // console.log("success");
                console.log(data);
                getUserList(data);
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    }
});
var getUserList = function (data) {
    userList = data.list;
    length = userList.length;
    start = (pageNo - 1) * pageSize;
    end = pageNo * pageSize;
    pageCount = Math.ceil(length / pageSize);
    if(end >= length){
        end = length;
    }
    // console.log("总条数："+length);
    // console.log("总页数："+pageCount);
    // console.log("当前页数："+pageNo);
    // console.log("一页数目："+pageSize);
    // console.log("显示第"+start+"条到第"+end+"条数据");
    // $("#pageCount").html("第"+pageNo+"页|共"+pageCount+"页");
    $("#pageNo").val(pageNo);
    $("#totalcount").html("当前共"+length+"条记录");
    if(pageNo == 1){
        $("#pre").removeClass("paging").addClass("notAllowed");
    }
    if(pageNo == pageCount){
        $("#next").removeClass("paging").addClass("notAllowed");
    }
    if(length == 0){
        insertEmpty();
    }
    else{
        insertHtml(start,end);
    }
};
var insertEmpty = function () {
    var strhtml = '<tr>';
    strhtml = strhtml + '<td colspan="11">暂无数据</td>';
    $("#userlist").append(strhtml);
};
var insertHtml = function (start,end) {
    for(var i=start;i<end;i++) {
        var userinfo = userList[i];
        var strhtml = '<tr id="' + i + '" arrId="' + i + '">';
        strhtml = strhtml + '<td><input type="checkbox" id="checkbox" class="checkbox" style="width: 20px;height: 20px;"></td>';
        strhtml = strhtml + '<td>' + userinfo.username + '</td>';
        strhtml = strhtml + '<td>' + userinfo.realname + '</td>';
        strhtml = strhtml + '<td>' + userinfo.phonumber + '</td>';
        strhtml = strhtml + '<td>' + userinfo.source + '</td>';
        if(userinfo.islock == 2){
            strhtml = strhtml + '<td class="lock">已冻结</td>';
        }
        else if(userinfo.state == 3){
            strhtml = strhtml + '<td>已认证</td>';
        }
        else if(userinfo.state == 4){
            strhtml = strhtml + '<td class="unAuthentication">未认证</td>';
        }
        else if(userinfo.state == 5){
            strhtml = strhtml + '<td class="fail">认证失败</td>';
        }
        strhtml = strhtml + '<td>' + userinfo.department + '</td>';
        strhtml = strhtml + '<td>' + userinfo.clinical_title + '</br>' + userinfo.teaching_title + '</td>';
        strhtml = strhtml + '<td>' + userinfo.type + '</td>';
        strhtml = strhtml + '<td>' + userinfo.createtime + '</td>';
        strhtml = strhtml + '<td><button id="changeuserinfo" class="operation">编辑</button><button id="forbid" class="operation">停用</button>';
        if(userinfo.state == 3){ //已认证
            strhtml = strhtml + '<button id="cancel" class="operation2">取消认证</button></td>';
        }
        else if(userinfo.state == 4 || userinfo.state == 5){ //未认证/认证失败
            strhtml = strhtml + '<button id="reset" class="operation2">审核</button></td>';
        }
        $("#userlist").append(strhtml);
    }
    // 编辑
    $("#userlist").on("click","tr td #changeuserinfo",function () {
        $('#update').show();
        $(".shandow").show().css('z-index', '11');
        $('body').css({
            "overflow-x":"hidden",
            "overflow-y":"hidden"
        });
        var i  = $(this).parent().parent().attr("arrId");
        var userinfo = userList[i];
        $("#update_username").val(userinfo.username);
        $("#update_phonumber").val(userinfo.phonumber);
        $("#update_realname").val(userinfo.realname);
        $("#update_idcard").val(userinfo.idcard);
        $("#update_type").val(userinfo.type);
        $("#update_department").val(userinfo.department);
        $("#update_job_number").val(userinfo.job_number);
        $("#update_clinical_title").val(userinfo.clinical_title);
        $("#update_teaching_title").val(userinfo.teaching_title);
        $("#update_goodat").val(userinfo.goodat);
        $("#update_description").val(userinfo.description);
        director = userinfo.director;
        Ustate = userinfo.state;
        if(userinfo.imgurl == ""){
            $("#update_userphoto").attr("src", "/pic/pic_userphoto_add.png");
        }
        else{
            $("#update_userphoto").attr("src", userinfo.imgurl);
        }
        if(userinfo.sex == "男"){
            $("input:radio[value='男']").attr('checked','true');
        }
        else if(userinfo.sex == "女"){
            $("input:radio[value='女']").attr('checked','true');
        }
        else if(userinfo.sex == "保密"){
            $("input:radio[value='保密']").attr('checked','true');
        }
        // 没有上级显示“无”，有则显示上级真实姓名
        if(userinfo.director == 0){
            $("#update_director").val("无");
        }
        else{
            $.ajax({
                type: "post",
                url: "/ysfwpt/selectUserByJobNumber",
                data: {
                    job_number: userinfo.director
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    $("#update_director").val(data.user.realname);
                },
                error: function (data) {
                    // console.log("error");
                    // console.log(data);
                }
            });
        }
        // 将输出的状态从数字转化为中文名称
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectUserStateById",
            data: {
                id: userinfo.state
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                $("#update_state").val(data.inputList.name);
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    });
    // 停用
    $("#userlist").on("click","tr td #forbid",function () {
        var r=confirm("请确认是否停用该用户账号？");
        if (r==true){
            var i  = $(this).parent().parent().attr("arrId");
            var userinfo = userList[i];
            var job_number = userinfo.job_number;
            $.ajax({
                type: "post",
                url: "/ysfwpt/updateIslock",
                data: {
                    job_number: job_number,
                    islock: "2"
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    window.location.href = "?"+username+"&&&&&&&1&1&20&&";
                },
                error: function (data) {
                    console.log("error");
                    // console.log(data);
                }
            });
        }
    });
    // 审核
    $("#userlist").on("click","tr td #reset",function () {
        $('#examine').show();
        $(".shandow").show().css('z-index', '11');
        $('body').css({
            "overflow-x":"hidden",
            "overflow-y":"hidden"
        });
        var i  = $(this).parent().parent().attr("arrId");
        var userinfo = userList[i];
        $("#examine_username").text(userinfo.username);
        $("#examine_phonumber").text(userinfo.phonumber);
        $("#examine_realname").text(userinfo.realname);
        $("#examine_sex").text(userinfo.sex);
        $("#examine_idcard").text(userinfo.idcard);
        $("#examine_type").text(userinfo.type);
        $("#examine_department").text(userinfo.department);
        $("#examine_job_number").text(userinfo.job_number);
        if(userinfo.imgurl == ""){
            $("#examine_userphoto").attr("src", "/pic/pic_userphoto.png");
        }
        else{
            $("#examine_userphoto").attr("src", userinfo.imgurl);
        }
        if(userinfo.director == 0){
            $("#examine_director").text("无");
        }
        else{
            $.ajax({
                type: "post",
                url: "/ysfwpt/selectUserByJobNumber",
                data: {
                    job_number: userinfo.director
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    $("#examine_director").text(data.user.realname);
                },
                error: function (data) {
                    // console.log("error");
                    // console.log(data);
                }
            });
        }
        if(userinfo.clinical_title == null || userinfo.clinical_title == ""){
            $("#examine_clinical_title").text("无");
        }
        else{
            $("#examine_clinical_title").text(userinfo.clinical_title);
        }
        if(userinfo.teaching_title == null || userinfo.teaching_title == ""){
            $("#examine_teaching_title").text("无");
        }
        else{
            $("#examine_teaching_title").text(userinfo.teaching_title);
        }
        if(userinfo.goodat == null || userinfo.goodat == ""){
            $("#examine_goodat").text("该用户暂未填写擅长内容");
        }
        else{
            $("#examine_goodat").text(userinfo.goodat);
        }
        if(userinfo.description == null || userinfo.description == ""){
            $("#examine_description").text("该用户暂未填写个人介绍");
        }
        else{
            $("#examine_description").text(userinfo.description);
        }
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectUserStateById",
            data: {
                id: userinfo.state
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                $("#examine_state").text(data.inputList.name);
            },
            error: function (data) {
                // console.log("error");
                // console.log(data);
            }
        });
    });
    // 取消认证
    $("#userlist").on("click","tr td #cancel",function () {
        var r=confirm("请确认是否取消该用户账号认证？");
        if (r==true){
            var i  = $(this).parent().parent().attr("arrId");
            var userinfo = userList[i];
            var job_number = userinfo.job_number;
            $.ajax({
                type: "post",
                url: "/ysfwpt/updateState",
                data: {
                    job_number: job_number,
                    state: "5"
                },
                success: function (data) {
                    // console.log("success");
                    // console.log(data);
                    window.location.href = "?"+username+"&&&&&&&1&1&20&&";
                },
                error: function (data) {
                    console.log("error");
                    // console.log(data);
                }
            });
        }
    });
};

var pId = [];
var pid;
// 全选
$("#all").click(function () {
    // 判断全选按钮的选中状态
    var xz = $(this).prop("checked");
    // 让class名为checkbox的选项的选中状态和全选按钮的选中状态一致
    $(".checkbox").prop("checked",xz);
    $(".checkbox").each(function() {
        if (this.checked) {
            var i = $(this).parent().parent().attr("arrId");
            if(pId.indexOf(userList[i].job_number) == -1){
                pId.push(userList[i].job_number);
            }
        }
        else{
            pId.splice(0,pId.length);
        }
    });
});
// 获取勾选的数据
$("#userlist").on("click","tr td #checkbox",function () {
    var i = $(this).parent().parent().attr("arrId");
    if($(this).is(':checked')) {
        if(pId.indexOf(userList[i].job_number) == -1){
            pId.push(userList[i].job_number);
        }
    }
    else{
        pId.splice($.inArray(userList[i].job_number,pId),1);
    }
});

// 搜索框下拉数据
$.ajax({
    type: "get",
    url: "/ysfwpt/selectDepartment",
    async: false,
    data: {

    },
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.department.length;i ++){
            $("#department_list ul").append("<li id=" + data.department[i].id + " class='list'>" + data.department[i].name + "</li>");
            $("#update_department_list ul").append("<li id=" + data.department[i].id + " class='update_list'>" + data.department[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectClinicalTitle",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.clinical_title.length;i ++){
            $("#clinical_title_list ul").append("<li id=" + data.clinical_title[i].id + " class='list'>" + data.clinical_title[i].name + "</li>");
            $("#update_clinical_title_list ul").append("<li id=" + data.clinical_title[i].id + " class='update_list'>" + data.clinical_title[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectTeachingTitle",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.teaching_title.length;i ++){
            $("#teaching_title_list ul").append("<li id=" + data.teaching_title[i].id + " class='list'>" + data.teaching_title[i].name + "</li>");
            $("#update_teaching_title_list ul").append("<li id=" + data.teaching_title[i].id + " class='update_list'>" + data.teaching_title[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectUserType",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.type.length;i ++){
            $("#type_list ul").append("<li id=" + data.type[i].id + " class='list'>" + data.type[i].name + "</li>");
            $("#update_type_list ul").append("<li id=" + data.type[i].id + " class='update_list'>" + data.type[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectUserSource",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.source.length;i ++){
            $("#source_list ul").append("<li id=" + data.source[i].id + " class='list'>" + data.source[i].name + "</li>");
        }
    }
});
$.ajax({
    type: "get",
    url: "/ysfwpt/selectUserState",
    async: false,
    data: {},
    success: function(data) {
        // console.log("success");
        // console.log(data);
        for(var i = 0;i < data.state.length;i ++){
            if(data.state[i].name != "已冻结"){
                $("#state").append("<button id=" + data.state[i].id + " class='state state_unchose'>" + data.state[i].name + "</button>");
            }
            if(state == (i+1)){
                $("#"+state+"").addClass('state_chose');
            }
        }
    }
});

// 设置下拉框宽度（输入框id1，下拉框id2）
var setWidth = function (id1,id2) {
    var width = $("#" + id1).css('width');
    $("#" + id2).css({
        "width": width
    });
};
// 点击搜索框
$("#department").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    // 显示时隐藏
    if($("#department_list").is(':visible')){
        $("#department_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 显示当前列表
        setWidth("department","department_list div");
        $("#department_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#department_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#department").val(this.innerHTML);
                $("#department_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#clinical_title").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#clinical_title_list").is(':visible')){
        $("#clinical_title_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        setWidth("clinical_title","clinical_title_list div");
        $("#clinical_title_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#clinical_title_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#clinical_title").val(this.innerHTML);
                $("#clinical_title_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#teaching_title").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#teaching_title_list").is(':visible')){
        $("#teaching_title_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        $("#teaching_title_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#teaching_title_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#teaching_title").val(this.innerHTML);
                $("#teaching_title_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#type").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#type_list").is(':visible')){
        $("#type_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        $("#type_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#type_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#type").val(this.innerHTML);
                $("#type_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#source").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡 
    if($("#source_list").is(':visible')){
        $("#source_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    else{
        $(".search_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        $("#source_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        var obj_lis = $("#source_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                $("#source").val(this.innerHTML);
                $("#source_list").slideUp(300);
                $(this).find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$(".state").click(function (e){
    // e.stopPropagation(); //阻止冒泡
    $("#state button").removeClass("state_chose").addClass("state_unchose");
    $(this).removeClass("state_unchose");
    $(this).addClass('state_chose');
    state = $(this).attr("id");
});
$("#pageSize").click(function (e){
    e.stopPropagation(); //阻止冒泡 
    if($("#pageSize_list").is(':visible')){
        $("#pageSize_list").slideUp(300);
    }
    else{
        $("#pageSize_list").slideDown(300);
        var obj_lis = $("#pageSize_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                pageSize = this.className;
                pageNo = 1;
                $("#pageSize").val(this.innerHTML);
                $("#pageSize_list").slideUp(300);
                refresh();
            }
        }
    }
});

// 搜索框点击任意选项后进行搜索
var object;
$(document).click(function(e) {
    object = $(e.target).attr("class");
    if(object == "list" || object == "state state_chose"){
        // 提交数据自动进行搜索
        refresh();
    }
});

// 点击×清空输入框，并重新搜索刷新页面
$(".icon_clear").click(function () {
    $(this).parent().find('input').val("");
    refresh();
});

//点击搜索
$("#search_button").click(function (){
    searchword = $("#search_input").val();
    refresh();
});

// 点击上一页/下一页
$("#pre").click(function (){
    if(pageNo == 1){
        console.log("首页")
    }
    else{
        pageNo = parseInt(pageNo) - parseInt(1);
        refresh();
    }
});
$("#next").click(function (){
    if(pageNo == pageCount){
        console.log("尾页")
    }
    else{
        pageNo = parseInt(pageNo) + parseInt(1);
        refresh();
    }
});

// 页码跳转输入框失去焦点后跳转页面
$("#pageNo").blur( function(){
    // console.log(pageNo);
    // 页码发生改变
    if(pageNo != $(this).val()){
        // 页码小于最小页码
        if($(this).val() < 1){
            pageNo = 1;
        }
        // 页码大于最大页码
        else if($(this).val() > pageCount){
            pageNo = pageCount;
        }
        else{
            pageNo = $(this).val();
        }
        refresh();
    }
});

// 下拉框定位函数（输入框id，下拉列表id）
var num = 0;
var time;
var resetList = function (id1,id2) {
    var top = $("#" + id1).offset().top;
    var left = $("#" + id1).offset().left;
    var width = $("#" + id1).css('width');
    var hide = $(window).scrollTop();
    $("#" + id2).css({
        "top": top - hide + 30,
        "left": left,
        "width": width
    });
    num++;
    console.log(num);
};
var stopList = function () {
    num = 0;
    clearInterval(time);
};

// 头像上传
$("#update_userphoto").click(function () {
    $('#update_file').attr("disabled",false);
    $("#update_file").click();
    $("#update_file").on("change",function() {
        var files = event.target.files, file;
        if (files && files.length > 0) {
            // 获取目前上传的文件
            file = files[0]; //文件大小校验的动作
            if (file.size > 1024 * 1024 * 2) {
                alert('图片大小不能超过 2MB!');
                return false;
            }
            var URL = window.URL || window.webkitURL;
            // 通过 file 生成目标 url
            var imgURL = URL.createObjectURL(file);
            //用attr将img的src属性改成获得的url
            $("#update_userphoto").attr("src", imgURL);
        }
    });
});
// 编辑弹窗页面点击输入框展示下拉框
$("#update_type").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡
    // 显示时隐藏
    if($("#update_type_list").is(':visible')){
        stopList();
        $("#update_type_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        stopList();
        $(".popup_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 将下拉框位置定位到点击的输入框下方
        time = setInterval(function () {
            resetList("update_type","update_type_list");
        },0);
        // 显示当前列表
        $("#update_type_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#update_type_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                stopList();
                $("#update_type").val(this.innerHTML);
                $("#update_type_list").slideUp(300);
                $("#update_type").parent().find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#update_department").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡
    // 显示时隐藏
    if($("#update_department_list").is(':visible')){
        stopList();
        $("#update_department_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        stopList();
        $(".popup_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 将下拉框位置定位到点击的输入框下方
        time = setInterval(function () {
            resetList("update_department","update_department_list");
        },0);
        // 显示当前列表
        setWidth("update_department","update_department_list div");
        $("#update_department_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#update_department_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                stopList();
                $("#update_department").val(this.innerHTML);
                $("#update_department_list").slideUp(300);
                $("#update_department").parent().find(".icon").css("transform","rotate(0deg)");
                $.ajax({
                    type: "post",
                    url: "/ysfwpt/selectDirectorByName",
                    data: {
                        name: this.innerHTML
                    },
                    success: function (data) {
                        // console.log("success");
                        // console.log(data);
                        director = data.department.director_id;
                        $.ajax({
                            type: "post",
                            url: "/ysfwpt/selectUserByJobNumber",
                            data: {
                                job_number: data.department.director_id
                            },
                            success: function (data) {
                                // console.log("success");
                                // console.log(data);
                                if(data.user != 0){
                                    $("#update_director").val(data.user.realname);
                                }
                                else{
                                    $("#update_director").val("无");
                                }
                            },
                            error: function (data) {
                                // console.log("error");
                                // console.log(data);
                            }
                        });
                    },
                    error: function (data) {
                        // console.log("error");
                        // console.log(data);
                    }
                });
            }
        }
    }
});
$("#update_clinical_title").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡
    // 显示时隐藏
    if($("#update_clinical_title_list").is(':visible')){
        stopList();
        $("#update_clinical_title_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        stopList();
        $(".popup_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 将下拉框位置定位到点击的输入框下方
        time = setInterval(function () {
            resetList("update_clinical_title","update_clinical_title_list");
        },0);
        // 显示当前列表
        setWidth("update_clinical_title","update_clinical_title_list div");
        $("#update_clinical_title_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#update_clinical_title_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                stopList();
                $("#update_clinical_title").val(this.innerHTML);
                $("#update_clinical_title_list").slideUp(300);
                $("#update_clinical_title").parent().find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
$("#update_teaching_title").parent().click(function (e){
    // e.stopPropagation(); //阻止冒泡
    // 显示时隐藏
    if($("#update_teaching_title_list").is(':visible')){
        stopList();
        $("#update_teaching_title_list").slideUp(300);
        $(this).find(".icon").css("transform","rotate(0deg)");
    }
    // 隐藏时显示
    else{
        // 隐藏所有列表
        stopList();
        $(".popup_input_option").slideUp(300);
        $(".icon").css("transform","rotate(0deg)");
        // 将下拉框位置定位到点击的输入框下方
        time = setInterval(function () {
            resetList("update_teaching_title","update_teaching_title_list");
        },0);
        // 显示当前列表
        $("#update_teaching_title_list").slideDown(300);
        $(this).find(".icon").css("transform","rotate(180deg)");
        // 获取点击的内容并显示在输入框内
        var obj_lis = $("#update_teaching_title_list ul li");
        for(var i = 0;i < obj_lis.length;i ++){
            obj_lis[i].onclick = function () {
                stopList();
                $("#update_teaching_title").val(this.innerHTML);
                $("#update_teaching_title_list").slideUp(300);
                $("#update_teaching_title").parent().find(".icon").css("transform","rotate(0deg)");
            }
        }
    }
});
//确定编辑
$("#update_bottom button:nth-child(1)").bind("click", function() {
    $("#update").hide();
    $(".shandow").hide().css('z-index','-1');
    $("#update_director").val(director);
    $("#update_state").val(Ustate);
    var formData = new FormData($("#updateform")[0]);
    $.ajax({
        type: "post",
        url: "/ysfwpt/updateUserInfo2",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            // console.log("success");
            // alert("更新成功！");
            refresh();
        },
        error: function (data) {

        }
    });
});
// 取消编辑
$("#update_top div,#update_bottom button:nth-child(2)").bind("click", function() {
    $("#update").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
    stopList();
    $(".popup_input_option").css("display","none");
    $(".icon").css("transform","rotate(0deg)");
});

// 根据认证状态确定是否展示认证失败理由
$('input:radio[name="examine_state"]').click(function(){
    var checkValue = $('input:radio[name="examine_state"]:checked').val();
    if(checkValue == 5){
        $('#failreason').css('display','');
    }
    else{
        $('#failreason').css('display','none');
    }
});
// 确定审核
$("#examine_bottom button:nth-child(1)").bind("click", function() {
    var state = $("input[name='examine_state']");
    var examine_job_number = $("#examine_job_number").html();
    var examine_state = $("input[name='examine_state']:checked").val();
    var examine_failreason = $("#examine_failreason").val();
    var isCheckRadio = false;
    for(var i = 0;i < state.length;i ++){
        if(state[i].checked){
            isCheckRadio=true;
            break;
        }
    }
    if(isCheckRadio == false || (examine_state == 5 && examine_failreason == "")){ // 没有选中认证状态 或者 认证状态为“认证失败”，没有填写认证失败理由
        alert("请完整填写带有*的信息");
        return false;
    }
    else{
        $.ajax({
            type: "post",
            url: "/ysfwpt/updateState",
            data: {
                job_number: examine_job_number,
                state: examine_state
            },
            success: function (data) {
                // console.log("success");
                // console.log(data);
                window.location.href = "?"+username+"&&&&&&&1&1&20&&";
            },
            error: function (data) {
                console.log("error");
                // console.log(data);
            }
        });
    }
});
// 取消审核
$("#examine_top div,#examine_bottom button:nth-child(2)").bind("click", function() {
    $("#examine").hide();
    $(".shandow").hide().css('z-index','-1');
    $('body').css({
        "overflow-x":"auto",
        "overflow-y":"auto"
    });
    $('input[type=radio][name="examine_state"]:checked').prop("checked", false);
    $("#failreason").css("display","none");
    $("#examine_failreason").val("");
});

var refresh = function () {
    var department = $("#department").val();
    var clinical_title = $("#clinical_title").val();
    var teaching_title = $("#teaching_title").val();
    var type = $("#type").val();
    var source = $("#source").val();
    var timefrom = $("#timefrom").val();
    var timeto = $("#timeto").val();
    //                            用户名             科室               临床职称               教学职称            类型          来源         姓名/联系方式       状态           页码          数据数目         开始时间        结束时间
    window.location.href = "?" + username + "&" + department + "&" + clinical_title + "&" + teaching_title + "&" + type + "&" + source + "&" + searchword + "&" + state + "&" + pageNo + "&" + pageSize + "&" + timefrom + "&" + timeto;
};